package com.ezjie.baselib.application;

import java.util.List;

import com.ezjie.baselib.util.BaseConfigure;
import com.ezjie.baselib.util.LogUtils;
import com.ezjie.baselib.util.NetworkUtil;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import android.app.Application;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.net.ConnectivityManager;

/**
 * 程序的入口点 可以存储一些短周期的变量
 * 
 * @author renyangyang
 * 
 */
public class MyApplication extends Application {

	public static MyApplication myApplication;
	
	@Override
	public void onCreate() {
		super.onCreate();
		myApplication = this;
		// 判断是否开启log日志输出
		initLogModel();
		// 设置网络
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkUtil.setNetState(cm, getApplicationContext());
		// 初始化客户端
		BaseConfigure.initClient(getApplicationContext());
		// 异步加载图片初始化
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				myApplication).build();
		ImageLoader.getInstance().init(config);
		
	}
	
	/**
	 * 根据是否为debug模式 判断是否开启Log打印
	 */
	private void initLogModel() {
		LogUtils.setDebugable(getApplicationContext());
		// 初始化输出日志级别
		BaseConfigure.setDebug(isApkDebugable(getApplicationContext()));
	}

	/**
	 * 根据是否打包apk获取debug标识, 统一管理日志输出
	 * 
	 * @param context
	 * @return
	 */
	protected boolean isApkDebugable(Context context) {
		try {
			ApplicationInfo info = context.getApplicationInfo();
			return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
		} catch (Exception e) {
			LogUtils.exception(e);
		}
		return false;
	}

	
}
